package com.jackrain.nea.fc.dl.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wuwenxue
 * @date 2018/05/22
 **/
@Mapper
public interface ViewDLInvAdjToF4Mapper {

    @SelectProvider(type = ViewDLInvAdjSql.class, method = "findInvAdjData")
    List<HashMap> selInvAdjData(@Param("id") Object[] id,
                                @Param("accepteStatus") Integer accepteStatus,
                                @Param("inv_adj_type") String inv_adj_type);

    @SelectProvider(type = ViewDLInvAdjSql.class, method = "findInvAdjItemData")
    List<HashMap> selInvAdjItemData(@Param("id") Long id);

    @SelectProvider(type = ViewDLInvAdjSql.class, method = "findInvAdjPosItemData")
    List<HashMap> selInvAdjPosItemData(@Param("id") Long id);

    @Select("SELECT ADJ_TYPE_ECODE FROM DL_B_ADJ_TYPE WHERE ID=#{id} AND ADJ_TYPE IN ('INV_ADJ','PAND','WORK_PICK','STORE_WORK_PICK')")
    String selAdjEcode(Long id);

    class ViewDLInvAdjSql {

        public String findInvAdjData(Map<String, Object> map) {
            Object[] id = (Object[]) map.get("id");
            Integer accepteStatus = (Integer) map.get("accepteStatus");
            String inv_adj_type = (String) map.get("inv_adj_type");
            return new SQL() {
                {
                    SELECT("ID,BILL_NO,CP_C_STORE_ECODE,BILL_DATE,ADJ_TYPE,REMARK,CREATIONDATE," +
                            "OWNERENAME,MODIFIERNAME,MODIFIERENAME,MODIFIEDDATE,PAND_DATE");
                    FROM("DL_B_INV_ADJ");
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < id.length; i++) {
                        sb.append(id[i] + ",");
                    }
                    sb.deleteCharAt(sb.length() - 1);
                    WHERE("ID IN (" + sb.toString() + ")");
//                    WHERE("ACCEPTE_STATUS=2");
                    if (null != accepteStatus) {
                        WHERE("ACCEPTE_STATUS = " + accepteStatus);
                    }
                    if (null != inv_adj_type) {
                        WHERE("INV_ADJ_TYPE = '" + inv_adj_type + "'");
                    }
                }
            }.toString();
        }

        public String findInvAdjItemData(Map<String, Object> map) {
            Long id = (Long) map.get("id");
            return new SQL() {
                {
                    SELECT("PS_C_SKU_ECODE,QTY_BILL,AMT_RETAIL");
                    FROM("DL_B_INV_ADJ_ITEM");
                    WHERE("DL_B_INV_ADJ_ID =" + id);
                }
            }.toString();
        }

        public String findInvAdjPosItemData(Map<String, Object> map) {
            Long id = (Long) map.get("id");
            return new SQL() {
                {
                    SELECT("B.BILL_NO AS BILL_NO,A.PS_C_SKU_ECODE AS PS_C_SKU_ECODE," +
                            "SUM(A.QTY_BILL) AS QTY_BILL,SUM(AMT_RETAIL) AS AMT_RETAIL");
                    FROM("DL_B_INV_ADJ_ITEM A");
                    JOIN("DL_B_INV_ADJ B ON A.DL_B_INV_ADJ_ID = B.ID AND B.INV_ADJ_TYPE='STORE_WORK_PICK' AND A.DL_B_INV_ADJ_ID="+id);
                    GROUP_BY("B.BILL_NO,A.PS_C_SKU_ECODE");
                }
            }.toString();
        }


    }


}
